package com.mypage.view.activity.beautMore;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.litesuits.android.async.Log;
import com.mypage.utils.NetStateUtils;
import com.mypage.utils.SaveTemporaryData;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DeleteGroupActivity extends BaseActivity {

    @Bind(R.id.deletePerson)
    TextView deletePerson;
    @Bind(R.id.cancelBtn)
    TextView cancelBtn;
    private String mGroupId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_delete_group);
        ButterKnife.bind(this);
        mGroupId = getIntent().getStringExtra("mId");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_delete_group;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        RunTimeManager.getInstance().setIsrem(false);
        finish();
        return super.onTouchEvent(event);
    }

    @OnClick({R.id.deletePerson, R.id.cancelBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.deletePerson:
                if (NetStateUtils.isNetworkConnected(this)){
                    deleteGroup();
                }else{
                    SaveTemporaryData.mIsnet = "no";
                }
                finish();
                break;
            case R.id.cancelBtn:
                finish();
                break;
        }
    }

    public void deleteGroup() {

        HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("binding", RunTimeManager.getInstance().getServerBinding());
        params.addBodyParameter("serviceName", "deleteOpportunityTeam");
        params.addBodyParameter("opportunityTeamId", mGroupId);
        Log.d("request删除业务机会小组url:", UrlManager.getInterfaceUrl() + "?binding=" + RunTimeManager.getInstance().getServerBinding() + "&serviceName=deleteOpportunityTeam");
        utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                Log.d("request删除业务机会小组Success：", arg0.result.toString());
                parseJson(arg0.result.toString());
            }
        });
    }

    public void parseJson(String str) {
        BusinChangeActivity.mToastBool = true;
        try {
            JSONObject object = new JSONObject(str);
            String info = object.getString("returnInfo");
            SaveTemporaryData.mToastStr = info;
            SaveTemporaryData.mSmart = "delete";
            GroupPdetailActivity.instance.finish();
            finish();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void finish() {
        super.finish();
        this.overridePendingTransition(R.anim.activity_close, 0);
    }

}
